package bb.lanxing.util;

import android.util.Patterns;

import com.blankj.utilcode.constant.RegexConstants;

import java.util.regex.Pattern;

import bb.lanxing.util.text.TextUtils;


public class Validator {
    private static final Pattern EMAIL_ADDRESS = Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}\\@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\" + ".[a-zA-Z0-9" +
            "][a-zA-Z0-9\\-]{0,25})+");

    public static boolean isValidEmail(String str) {
        return EMAIL_ADDRESS.matcher(str).matches();
    }

    public static boolean isPhoneNumber(String str) {
        return Patterns.PHONE.matcher(str).matches();
    }

    public static boolean isValidCardNo(String str) {
        return !TextUtils.isEmptyOrNull(str) && (str.matches(RegexConstants.REGEX_ID_CARD15) || str.matches("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(" +
                "([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X|x)$"));
    }

    public static boolean isValidUsername(String str) {
        if (str == null || str == "") {
            return false;
        }
        String trim = str.trim();
        for (int i = 0; i < trim.length(); i++) {
            char charAt = trim.charAt(i);
            if (!isChineseChar(charAt) && !isDigit(charAt) && !isEnglishChar(charAt)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isChineseChar(char c) {
        return String.valueOf(c).matches("[一-龥]");
    }

    public static boolean isDigit(char c) {
        return String.valueOf(c).matches("[0-9]");
    }

    public static boolean isEnglishChar(char c) {
        return String.valueOf(c).matches("[a-zA-Z]");
    }
}
